package com.ramesh.fragmenttabhostdemo;

import java.io.File;

import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

public class WhereToGoFragment extends Fragment {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	}

	private void initShowPdf() {
		TextView show_pdf = (TextView)getView().findViewById(R.id.show_pdf);
		show_pdf.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (canDisplayPdf()) {
					openPdfreader();
				} else {
					AlertPdfReaderNotExist();
				}
			}
		});
	}		

	private void AlertPdfReaderNotExist() {
		//AlertDialog dialog = new AlertDialog.Builder(this).setTitle("Argh").setMessage("Watch out!").setNeutralButton("Close", null).show();  
		AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getView().getContext());
		alertDialogBuilder.setTitle("Error read PDF");

		alertDialogBuilder
		.setMessage("Please install PDF reader to show the pdf file!")
		.setCancelable(false)
		.setNeutralButton("Close", null);
		// create alert dialog
		AlertDialog alertDialog = alertDialogBuilder.create();

		// show it
		alertDialog.show();
	}

	private void openPdfreader() {
		File file = Pdfreader_util.getFile("itinerary_saudi_arabia.pdf", getView());
		Intent intent = new Intent(Intent.ACTION_VIEW);
		intent.setDataAndType(
				Uri.parse("file://" + file.getAbsolutePath()), "application/pdf");
		try 
		{
			startActivity(intent);
		} 
		catch (ActivityNotFoundException e) 
		{
			Toast.makeText(getView().getContext(), "Error opening pdf reader", Toast.LENGTH_SHORT).show();
		}

	}


	public boolean canDisplayPdf() {
	    PackageManager packageManager = getView().getContext().getPackageManager();
	    Intent testIntent = new Intent(Intent.ACTION_VIEW);
	    testIntent.setType("application/pdf");
	    if (packageManager.queryIntentActivities(testIntent, PackageManager.MATCH_DEFAULT_ONLY).size() > 0) {
	        return true;
	    } else {
	        return false;
	    }
	}

	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View v = LayoutInflater.from(getActivity()).inflate(R.layout.where_to_go_page,
				null);
		//text = (TextView) v.findViewById(R.id.text);
		if (getArguments() != null) {
			//
			try {
				getArguments().getString("key");
				///text.setText("Current Tab is: " + value);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return v;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		initShowPdf();
		initMapFragment();
	}

	private void initMapFragment() {
		// TODO Auto-generated method stub
		TextView map = (TextView)getView().findViewById(R.id.show_map);
		map.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				FragmentManager fragmentManager = getFragmentManager();
				FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

				CustomMapFragment mMapFragment = new CustomMapFragment();
				fragmentTransaction.replace(R.id.realtabcontent, mMapFragment);
				//provide the fragment ID of your first fragment which you have given in
				//fragment_layout_example.xml file in place of first argument
				fragmentTransaction.addToBackStack(null);
				fragmentTransaction.commit();
			}
		});

	}
}
